import * as $group from '@/api/group'
export const APP_SET_CURRENT_PATH = 'APP_SET_CURRENT_PATH'
export const APP_SET_GROUP_ID = 'APP_SET_GROUP_ID'
export const APP_GET_VERSION = 'APP_GET_VERSION'
export const APP_SET_IS_APP_SHOW = 'APP_SET_IS_APP_SHOW'

const state = {
  version: 0,
  groupId: null,
  currentPath: '',
  isAppShow: false
}

const getters = {
  version: state => state.version,
  groupId: state => state.groupId,
  isAppShow: state => state.isAppShow,
  currentPath: state => state.currentPath
}

const actions = {
  async fetchGroup ({ commit, state, rootState }, { encryptedData, iv, groupOwnerId }) {
    let res = await $group.getInfo({encryptedData, iv, groupOwnerId})
    let groupId = res && res.id ? res.id : 0
    commit(APP_SET_GROUP_ID, groupId)
    return groupId
  },
  async setCurrentPath ({ commit, state, rootState }, path) {
    commit(APP_SET_CURRENT_PATH, path)
  },
  setGroupId ({ commit, state, rootState }, gid) {
    commit(APP_SET_GROUP_ID, gid)
  },
  setIsAppShow ({ commit, state, rootState }, flag) {
    commit(APP_SET_IS_APP_SHOW, flag)
  }
}

const mutations = {
  APP_SET_GROUP_ID (state, gid) {
    state.groupId = gid
  },
  APP_SET_IS_APP_SHOW (state, flag) {
    state.isAppShow = flag
  },
  APP_SET_CURRENT_PATH (state, path) {
    state.currentPath = path
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}
